package cn.xfdzx.android.apps.shop.presenter;

import java.io.File;
import java.io.IOException;
import java.net.Proxy;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.LoginBean;
import cn.xfdzx.android.apps.shop.bean.UploadImgBeen;
import cn.xfdzx.android.apps.shop.contract.LoginContract;
import cn.xfdzx.android.apps.shop.contract.UploadImageContract;
import cn.xfdzx.android.apps.shop.model.MainModel;
import cn.xfdzx.android.apps.shop.model.UploadModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import cn.xfdzx.android.apps.shop.util.HttpUtils;
import cn.xfdzx.android.apps.shop.util.UtilsLog;
import io.reactivex.functions.Consumer;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class UploadPresenter extends BasePresenter<UploadImageContract.View> implements UploadImageContract.Presenter {

    private UploadImageContract.Model model;

    public UploadPresenter() {
        model = new UploadModel();
    }


    @Override
    public void upload(File file) {
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();

        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        model.upload(filePart)
                .compose(RxScheduler.<UploadImgBeen>Flo_io_main())
                .as(mView.<UploadImgBeen>bindAutoDispose())
                .subscribe(new Consumer<UploadImgBeen>() {
                    @Override
                    public void accept(UploadImgBeen bean) throws Exception {
                        mView.onUploadSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
//        upLoadFiles("https://pages.xfdzx.cn/addons/shimmer_liveshop/index.php/api/6/3/plugin/merch/common/apply/uploadImg", file, new ConnectListener() {
//            @Override
//            public void connectSuccess(String result) {
//
//            }
//
//            @Override
//            public void connectFailed(String error) {
//
//            }
//        });

    }


}
